package com.example.a12502.aihome;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.a12502.aihome.history.HistoryFragmentPagerAdapter;
import com.example.a12502.aihome.history.JianquanFragment;
import com.example.a12502.aihome.history.PmFragment;
import com.example.a12502.aihome.history.WetFragment;

import java.util.ArrayList;

public class HistoryFragment extends Fragment implements RadioGroup.OnCheckedChangeListener{


    private ViewPager mPager;
    private ArrayList<Fragment> fragmentList;
    private RadioButton jiaquan,pm2_5,wet;
    private RadioGroup bottomGroup;


    //三个界面的Fragment
    private JianquanFragment jianquanFragment;
    private PmFragment pmFragment;
    private WetFragment wetFragment;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_history, container, false);
        initViews(view);
        initViewPager(view);
        return view;
    }
    public void initViews(View view){
        mPager=view.findViewById(R.id.history_viewPager);
        bottomGroup = view.findViewById(R.id.history_radiogroup);
        jiaquan =  view.findViewById(R.id.history_jiaquan);
        pm2_5 =  view.findViewById(R.id.history_pm2_5);
        wet =  view.findViewById(R.id.history_wet);

        bottomGroup.setOnCheckedChangeListener(this);
    }

    private void initViewPager(View view){
        jianquanFragment = new JianquanFragment();
        pmFragment = new PmFragment();
        wetFragment = new WetFragment();

        ViewPager viewPager=view.findViewById(R.id.history_viewPager);

        fragmentList=new ArrayList<Fragment>();
        fragmentList.add(0,jianquanFragment);
        fragmentList.add(1,pmFragment);
        fragmentList.add(2,wetFragment);

        //ViewPager设置适配
        mPager.setAdapter(new HistoryFragmentPagerAdapter(getChildFragmentManager(),viewPager, fragmentList));
        //ViewPager显示第一个Fragment
        mPager.setCurrentItem(0);
        //ViewPager页面切换监听
        mPager.addOnPageChangeListener(new HistoryFragment.MyOnPageChangeListener());
    }
    /**
     *ViewPager切换Fragment,RadioGroup做相应变化
     */
    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position){
                case 0:
                    bottomGroup.check(R.id.history_jiaquan);
                    break;
                case 1:
                    bottomGroup.check(R.id.history_pm2_5);
                    break;
                case 2:
                    bottomGroup.check(R.id.history_wet);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * RadioButton切换Fragment
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.history_jiaquan:
                //ViewPager显示第一个Fragment且关闭页面切换动画效果
                mPager.setCurrentItem(0,true);
                break;
            case R.id.history_pm2_5:
                mPager.setCurrentItem(1,true);
                break;
            case R.id.history_wet:
                mPager.setCurrentItem(2,true);
                break;
        }
    }


}
